﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevTyr.Mvvm.ViewModel;
using DevTyr.Mvvm.Locator;
using DevTyr.Mvvm.Commanding;
using DevTyr.Mvvm.Messaging;
using System.Windows.Input;
using System.Windows.Controls;
using System.Collections.ObjectModel;

namespace SerienNewsReaderMVVM.ViewModel
{
    [LocatorAttribute("MainViewModel")]
    public class MainViewModel : ViewModelBase
    {
        private ObservableCollection<MenuItem> menu = default(ObservableCollection<MenuItem>);

        /// <summary>
        /// Command to apply a filter
        /// </summary>
        private RelayCommand applyFilterCommand;

        /// <summary>
        /// Gets an instnace of <see cref="RelayCommand"/> to apply a filter
        /// </summary>
        public ICommand ApplyFilterCommand
        {
            get
            {
                if (this.applyFilterCommand == null)
                {
                    this.applyFilterCommand = new RelayCommand(this.SendApplyFilterCommand);
                }
                return this.applyFilterCommand;
            }
        }

        /// <summary>
        /// Sends the <see cref="ApplyFilterCommand"/> with the filter criteria
        /// </summary>
        /// <param name="filter"></param>
        private void SendApplyFilterCommand(object filter)
        {
            Messenger.Instance.Send<string>(filter as string);
        }

        public MainViewModel()
        {
            this.PrepareFilterMenue();
        }

        private void PrepareFilterMenue()
        {
            List<string> entries = this.ParseMenuEntries();
            List<MenuItem> items = new List<MenuItem>();
            if (entries.Count == 0)
            {
                return;
            }
            else
            {
                entries.Sort();
                foreach (string entry in entries)
                {
                    if (string.IsNullOrEmpty(entry))
                    {
                        continue;
                    }
                    else
                    {
                        MenuItem item = new MenuItem();
                        item.Header = entry;
                        item.Command = this.ApplyFilterCommand;
                        item.CommandParameter = item.Header;
                        items.Add(item);
                        //base.RaisePropertyChanged("Menu");
                    }
                }

                MenuItem allMySeries = new MenuItem();
                allMySeries.Header = "All my series";
                allMySeries.Command = this.ApplyFilterCommand;
                allMySeries.CommandParameter = allMySeries.Header;
                items.Add(allMySeries);

                MenuItem allSeries = new MenuItem();
                allSeries.Header = "All series";
                allSeries.Command = this.ApplyFilterCommand;
                allSeries.CommandParameter = allSeries.Header;
                items.Add(allSeries);

                if (items.Count > 0)
                {
                    this.Menu = new ObservableCollection<MenuItem>(items);
                }
            }
        }

        private List<string> ParseMenuEntries()
        {
            string[] entries = SerienNewsReaderMVVM.config.Default.Series.Split(';');
            return entries.ToList<string>();
        }

        /// <summary>
        /// Gets or sets the items that are displayed in the menu
        /// </summary>
        public ObservableCollection<MenuItem> Menu
        {
            get
            {
                return this.menu;
            }
            set
            {
                if (value != this.menu)
                {
                    this.menu = value;
                    base.RaisePropertyChanged("Menu");
                }
            }
        }
    }
}
